﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using YY.UI.BaseDataSvc;
using YY.Common;

namespace YY.UI
{
    public partial class CountFeeFrm : BaseFrm
    {
        /// <summary>
        /// 用户源数据临时变量
        /// </summary>
        IEnumerable<TB_User> users = null;

        public CountFeeFrm()
        {
            InitializeComponent();
            this.BaseDataProxy = new YY.UI.BaseDataSvc.BaseDataClient();
        }

        DataTable dt = null;
        private void btnSearch_Click(object sender, EventArgs e)
        {
            //this.BaseDataProxy.Open();
            DateTime dtStart = this.dtpSDate.Value.Date;
            DateTime dtEnd = this.dtpEDate.Value.AddDays(1).Date.AddSeconds(-1);
            int? bizMan = null;
            if (this.cbBoxOperator.SelectedItem != null)
                bizMan = (this.cbBoxOperator.SelectedItem as TB_User).id;
            this.dt = this.BaseDataProxy.GetCountFee(dtStart, dtEnd, bizMan);
            if (this.dt != null && this.dt.Rows.Count > 0)
            {
                DataRow dr = dt.NewRow();
                dr[1] = "合计：";
                ArrayList objs = new ArrayList { };
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    int num = 3;

                    for (int j = num; j < dt.Columns.Count; j++)
                    {
                        if (i == 0)
                            objs.Add(dt.Rows[i][j]);
                        else
                        {
                            if (!Common.IsDgvCellNull(dt.Rows[i][j]))
                            {
                                decimal m = Convert.ToDecimal(dt.Rows[i][j]);
                                objs[j - num] = Common.IsDgvCellNull(objs[j - num]) ? m : Convert.ToDecimal(objs[j - num]) + m;
                            }
                        }
                        if (i == dt.Rows.Count - 1)
                            dr.SetField(j, objs[j - num]);
                        //excel.SetCellValue(excel.ws, rowTotal, j + 1, objs[j - 6]);
                    }
                }
                dt.Rows.Add(dr);
                dt.AcceptChanges();
            }
            this.dataGridView1.DataSource = dt;
            if (dt != null)
                this.dataGridView1.Columns["OrderId"].Visible = false;
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                ExcelCommon excel = new ExcelCommon();
                try
                {
                    DataTable exportDt = dt.Copy();
                    exportDt.Columns.Remove("OrderId");
                    exportDt.AcceptChanges();
                    excel.Create();
                    excel.ws = excel.AddSheet("费用汇总表");

                    excel.SetCellValue(excel.ws, 1, 1, "费用汇总表");//row1
                    excel.UniteCells(excel.ws, 1, 1, 1, exportDt.Columns.Count);
                    excel.SetCellProperty(excel.ws, 1, 1, 1, exportDt.Columns.Count, 30, true, "宋体", Microsoft.Office.Interop.Excel.Constants.xlColor3, Microsoft.Office.Interop.Excel.Constants.xlCenter, false);

                    for (int c = 0; c < exportDt.Columns.Count; c++)
                    {
                        excel.SetCellValue(excel.ws, 2, c + 1, exportDt.Columns[c].ColumnName);
                        excel.SetCellProperty(excel.ws, 2, c + 1, 2, c + 1, 13, true, "宋体", Microsoft.Office.Interop.Excel.Constants.xlColor3, Microsoft.Office.Interop.Excel.Constants.xlCenter, false);
                    }
                    excel.AddTable(exportDt, excel.ws, 3, 1);
                    excel.SetRangeGrid(excel.ws, 2, 1, 2 + exportDt.Rows.Count, exportDt.Columns.Count, Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic);

                    excel.ColumnsAutoFit(excel.ws, 1, 1, 2 + exportDt.Rows.Count, exportDt.Columns.Count);
                    excel.app.Visible = true;

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    excel.Dispose();
                }
                finally
                {

                }
            }
        }

        private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            Common.ShowDataGridViewRowNo(sender);
        }

        private void dataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            Common.ShowDataGridViewRowNo(sender);
        }

        private void CountFeeFrm_Load(object sender, EventArgs e)
        {
            this.BaseDataProxy.Open();
            this.dtpEDate.Value = DateTime.Now;
            this.dtpSDate.Value = DateTime.Now.AddDays(-7);
            this.users = this.BaseDataProxy.FindUsers(null, null);
            if (this.users.Any())
            { this.users = this.users.Where(p => p.role == (int)EnumCommon.SysRoleType.业务员 || p.role == (int)EnumCommon.SysRoleType.主管经理); }
            Common.ComboBoxBindDatas<TB_User>(this.cbBoxOperator, this.users, "userName", "id");
            this.cbBoxOperator.KeyPress += new KeyPressEventHandler(cbBoxUser_KeyPress);
        }

        /// <summary>
        /// 用户选择框回车事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbBoxUser_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                ComboBox cbBox = sender as ComboBox;
                if (this.users != null)
                {
                    TB_User[] users = this.users.Where(p => p.userName.Contains(cbBox.Text) || p.code.Contains(cbBox.Text)).ToArray();
                    Common.ComboBoxMatchUser(cbBox, users, this);
                }
            }
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            this.cbBoxOperator.SelectedIndex = -1;
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dataGridView1.CurrentRow != null)
            {
                object orderId = this.dataGridView1.CurrentRow.Cells["OrderId"].Value;
                if (orderId != null)
                {
                    EditOrderFrm frm = new EditOrderFrm();
                    frm.OrderId = Convert.ToInt32(orderId);
                    frm.EditMode = true;
                    frm.Show(this);
                }
            }
        }
    }
}
